<template>
  <div>
    <el-card>
      <div>
        <el-row type="flex" justify="space-between">
          <el-button type="primary" size="small" @click="addAgent">添加经纪</el-button>
          <el-col :span="12">
            <el-input v-model="searchName" placeholder="请输入关键字">
              <el-button slot="append" icon="el-icon-search" @click="searchFn" />
            </el-input>
          </el-col>
        </el-row>
      </div>
      <div class="table-content">
        <el-table :data="tableData" border>
          <el-table-column label="图片" prop="image">
            <template v-slot="{row}">
              <img style="width:180px" :src="row.image" alt="">
            </template>
          </el-table-column>
          <el-table-column label="名称" prop="name" />
          <el-table-column label="邮箱" prop="email" />
          <el-table-column label="电话" prop="phone" />
          <el-table-column label="地址" prop="address" />
          <el-table-column label="介绍" prop="desc" />
          <el-table-column v-slot="{row}" label="操作">
            <el-button size="small" type="primary" @click="editFn(row.id)">编辑</el-button>
            <el-button size="small" type="danger" @click="delFn(row.id)">删除</el-button>
          </el-table-column>
        </el-table>
        <el-row type="flex" justify="center">
          <el-pagination
            layout="prev, pager, next"
            :page-size="pageSize"
            :total="total"
            @current-change="changePage"
          />
        </el-row>
      </div>
    </el-card>
    <!-- 弹窗 -->
    <AddDia ref="addDia" :get-agents="getAgents" :show-dialog.sync="showDialog" :cur-data="curData" />
    <!-- <el-dialog :title="formData.id?'编辑经纪':'新增经纪'" :visible.sync="showDialog" @close="closeFn">
      <el-form ref="form" :rules="rules" :model="formData" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="formData.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="formData.phone" placeholder="请输入电话号码" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="formData.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="描述" prop="desc">
          <el-input v-model="formData.desc" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="封面">
          <ImageUpload ref="imgupload" />
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-row type="flex" justify="center">
          <el-button size="small" type="primary" @click="confirmFn">确认</el-button>
          <el-button size="small" @click="closeFn">取消</el-button>
        </el-row>
      </span>
    </el-dialog> -->
  </div>
</template>

<script>
import { getAgents, delAgent, getAgentById, getAgentCount } from '@/api/agent'
import AddDia from './components/add-dia.vue'
export default {
  components: { AddDia },
  data() {
    return {
      showDialog: false,
      pageSize: 2,
      tableData: [],
      curData: {},
      page: 1,
      total: 0,
      searchName: ''
    }
  },
  async created() {
    // 获取经纪列表
    this.getAgents()
    // 获取经纪数量
    this.getAgentCount()
  },
  methods: {
    // 搜索
    async searchFn() {
      const res = await getAgents({
        name_contains: this.searchName,
        _limit: this.pageSize,
        _start: this.pageSize * (this.page - 1)
      })
      this.getAgentCount()
      this.tableData = res.data
      console.log(104, res)
    },
    async getAgentCount() {
      const res = await getAgentCount({
        name_contains: this.searchName
      })
      this.total = res.data
    },
    // 编辑面经
    async editFn(id) {
      console.log(id)
      this.showDialog = true
      const res = await getAgentById(id)
      this.curData = res.data
      this.$nextTick(() => {
        this.$refs.addDia.edit()
      })
      // 图片回显
      // this.$nextTick(() => {
      //   this.$refs.imgupload.fileList.push({
      //     url: res.data.image
      //   })
      // })
    },
    // 删除面经
    delFn(id) {
      this.$confirm('确认删除这条数据吗？', '删除', {
        type: 'warning'
      }).then(async() => {
        // 调用接口 删除面经
        await delAgent(id)
        // 更新面经列表
        this.getAgents()
        if (this.page !== 1 && this.tableData.length === 1) {
          this.page--
          this.getAgents()
          this.getAgentCount()
        }
      }).catch(err => {
        console.log('取消', err)
      })
    },
    // 获取面经列表
    async getAgents() {
      const res = await getAgents({
        _limit: this.pageSize,
        _start: this.pageSize * (this.page - 1)
      })
      this.tableData = res.data
      console.log(151, res.data)
      this.getAgentCount()
    },
    // 新增经纪
    addAgent() {
      this.showDialog = true
    },

    changePage(newpage) {
      this.page = newpage
      this.getAgents()
    }
  }
}
</script>

<style>
.table-content{
  margin-top: 10px;
}
</style>
